{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "This example demonstrates how to run AgentQL script in online development environment like [Google Colaboratory](https://colab.research.google.com/)."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "cOG_eI8DY3co"
      },
      "source": [
        "Install [AgentQL](https://pypi.org/project/agentql/) library"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "mGqCFOMozoqj"
      },
      "outputs": [],
      "source": [
        "%pip install agentql"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Install the Playwright dependency required by AgentQL."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "!playwright install chromium"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "You can [store](https://medium.com/@parthdasawant/how-to-use-secrets-in-google-colab-450c38e3ec75) keys in Google Colab's Secrets."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "5a405Rl5lqT1"
      },
      "outputs": [],
      "source": [
        "import os\n",
        "\n",
        "from google.colab import user_data\n",
        "\n",
        "os.environ[\"AGENTQL_API_KEY\"]=userdata.get('AGENTQL_API_KEY')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Run AgentQL script. Please note that online environment like Google Colab only supports **asynchronous version** of AgentQL."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "W3IIXkPJ0iIX"
      },
      "outputs": [],
      "source": [
        "import agentql\n",
        "from playwright.async_api import async_playwright\n",
        "\n",
        "\n",
        "URL = \"https://scrapeme.live/shop\"\n",
        "\n",
        "SEARCH_QUERY = \"\"\"\n",
        "{\n",
        "    search_products_box\n",
        "}\n",
        "\"\"\"\n",
        "\n",
        "STOCK_NUMBER_QUERY = \"\"\"\n",
        "{\n",
        "    number_in_stock\n",
        "}\n",
        "\"\"\"\n",
        "\n",
        "async with async_playwright() as playwright, await playwright.chromium.launch() as browser:\n",
        "    # Create a new page in the browser and wrap it to get access to the AgentQL's querying API\n",
        "    page= await agentql.wrap_async(browser.new_page())\n",
        "\n",
        "    await page.goto(URL)\n",
        "\n",
        "    # Use query_elements() method to locate the search box and search button from the page\n",
        "    response = await page.query_elements(SEARCH_QUERY)\n",
        "\n",
        "    # Use Playwright's API to fill the search box and press Enter\n",
        "    await response.search_products_box.type(\"Charmander\")\n",
        "    await page.keyboard.press(\"Enter\")\n",
        "\n",
        "    # Use query_data() method to fetch the president name from the page\n",
        "    response = await page.query_data(STOCK_NUMBER_QUERY)\n",
        "\n",
        "    print(response)"
      ]
    }
  ],
  "metadata": {
    "colab": {
      "authorship_tag": "ABX9TyN+D8bbAfsSzsGIEJwCw5ln",
      "include_colab_link": true,
      "provenance": []
    },
    "kernelspec": {
      "display_name": "Python 3",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.11.4"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}
